package com.huawei.hianalytics.f.f;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Pair;
import com.huawei.hianalytics.f.b.a;
import com.huawei.hianalytics.f.b.d;
import com.huawei.hianalytics.f.g.h;
import com.huawei.hianalytics.f.g.j;
import com.huawei.hianalytics.f.g.k;
import com.huawei.hianalytics.g.b;
import com.huawei.hms.framework.network.restclient.hwhttp.route.IPRoute;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONObject;

public class c {
    private Context a;
    private String b;
    private com.huawei.hianalytics.f.b.c[] c;
    private String d;
    private String e;

    c(Context context, String str, com.huawei.hianalytics.f.b.c[] cVarArr, String str2, String str3) {
        this.a = context;
        this.b = str;
        this.c = cVarArr;
        this.d = str2;
        this.e = str3;
    }

    private void a(List<d> list, String str, String str2) {
        int size = list.size();
        if (size > 0) {
            while (size > 0) {
                int i = size - (size > IPRoute.INTERVAL ? IPRoute.INTERVAL : size);
                b(list.subList(i, size), str, str2);
                size = i;
            }
            return;
        }
        b.b("HiAnalytics/event", "no events to send,TAG : %s,TYPE: %s", str2, str);
    }

    private void a(com.huawei.hianalytics.f.b.c[] cVarArr, String str, String str2) {
        b.a("EventDataHandler", "choiceHandleEvents TAG : %s,TYPE: %s", str2, str);
        a(o.a(cVarArr), str, str2);
    }

    @SafeVarargs
    private final void a(com.huawei.hianalytics.f.b.c[] cVarArr, List<com.huawei.hianalytics.f.b.c>... listArr) {
        for (com.huawei.hianalytics.f.b.c cVar : cVarArr) {
            String str = "";
            CharSequence g = cVar.g();
            if (TextUtils.isEmpty(g) || "oper".equals(g)) {
                listArr[0].add(cVar);
            } else if ("maint".equals(g)) {
                listArr[1].add(cVar);
            } else if ("preins".equals(g)) {
                listArr[2].add(cVar);
            } else if ("diffprivacy".equals(g)) {
                listArr[3].add(cVar);
            }
        }
    }

    private void b(List<d> list, String str, String str2) {
        if (list.size() > 0) {
            List arrayList = new ArrayList();
            JSONArray jSONArray = new JSONArray();
            long currentTimeMillis = System.currentTimeMillis();
            long g = ((long) com.huawei.hianalytics.a.c.g(str2, str)) * HwAccountConstants.CHECK_SITE_COUNTRY_DURATION;
            long length = h.c(this.a, "backup_event").length();
            long j = length;
            for (d dVar : list) {
                if (dVar.a(currentTimeMillis, g)) {
                    length = j;
                } else {
                    a a = dVar.a();
                    arrayList.add(a);
                    JSONObject a2 = a.a();
                    length = a.i() + j;
                    jSONArray.put(a2);
                }
                j = length;
            }
            if (arrayList.size() <= 0 || jSONArray.length() <= 0) {
                b.c("EventDataHandler", "Not have data need to send.TAG : %s,TYPE: %s", str2, str);
                return;
            }
            SharedPreferences d = h.d(this.a, "backup_event");
            String str3 = this.e;
            if (TextUtils.isEmpty(this.e)) {
                str3 = UUID.randomUUID().toString().replace("-", "");
            }
            if (h.a(this.a, "backup_event", 5242880) || j >= 5242880) {
                b.b("EventDataHandler", "backup file reach max limited size, discard new event : " + j);
            } else {
                String a3 = j.a(str2, str, str3);
                b.b("EventDataHandler", "Update data cached into backup,spKey: " + a3 + ". backupDataLength: " + j);
                try {
                    h.a(d, a3, jSONArray.toString());
                } catch (OutOfMemoryError e) {
                    b.c("EventDataHandler", "the backup data is too big,toString() -> OOM,backupDataLength: " + j);
                }
            }
            k.b(new a(this.a, arrayList, str, str2, this.d, str3));
            return;
        }
        b.b("HiAnalytics/event", "empty event data, no need to send,TAG : %s,TYPE: %s", str2, str);
    }

    public void a() {
        b.b("EventDataHandler", "handler event report...");
        Pair a = j.a(this.b);
        if ("_default_config_tag".equals(a.first)) {
            List arrayList = new ArrayList();
            List arrayList2 = new ArrayList();
            List arrayList3 = new ArrayList();
            List arrayList4 = new ArrayList();
            a(this.c, arrayList, arrayList2, arrayList3, arrayList4);
            if (arrayList.size() > 0) {
                a((com.huawei.hianalytics.f.b.c[]) arrayList.toArray(new com.huawei.hianalytics.f.b.c[arrayList.size()]), "oper", "_default_config_tag");
            }
            if (arrayList2.size() > 0) {
                a((com.huawei.hianalytics.f.b.c[]) arrayList2.toArray(new com.huawei.hianalytics.f.b.c[arrayList2.size()]), "maint", "_default_config_tag");
            }
            if (arrayList3.size() > 0) {
                a((com.huawei.hianalytics.f.b.c[]) arrayList3.toArray(new com.huawei.hianalytics.f.b.c[arrayList3.size()]), "preins", "_default_config_tag");
            }
            if (arrayList4.size() > 0) {
                a((com.huawei.hianalytics.f.b.c[]) arrayList4.toArray(new com.huawei.hianalytics.f.b.c[arrayList4.size()]), "diffprivacy", "_default_config_tag");
                return;
            }
            return;
        }
        a(this.c, (String) a.second, (String) a.first);
    }
}
